from appium import webdriver
from appium.options.common import AppiumOptions


class Driver(object):
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, "_instance"):
            cls._instance = object().__new__(cls)
        return cls._instance
    def __init__(self):
        if not hasattr(self, "driver"):
            self.driver = None

    def create_driver(self):
        """创建driver"""
        options = AppiumOptions()
        options.load_capabilities({
            "platformName": "Android",
            "appium:automationName": "UiAutomator2",
            "appium:ensureWebviewsHavePages": True,
            "appium:nativeWebScreenshot": True,
            "appium:newCommandTimeout": 3600,
            "appium:noReset": True,
            "appium:connectHardwareKeyboard": True,
            "appium:appPackage": "com.xiaomi.shop",
            "appium:appActivity": ".activity.MainTabActivity "
        })
        self.driver = webdriver.Remote("http://127.0.0.1:4723", options=options)
        self.driver.implicitly_wait(20)
        self.driver.wait_activity("com.xiaomi.shop.activity.MainTabActivity", timeout=20)


    def get_driver(self):
        self.create_driver()
        return self.driver



DRIVER = Driver().get_driver()
print(DRIVER)
